//
//  HDAppointViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/13.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDAppointViewController.h"
#import "HDAppointTableViewController.h"

@interface HDAppointViewController ()
@property(nonatomic,strong)HDAppointTableViewController *appointList;
@property(nonatomic,strong)NSString *orderId;
@property (nonatomic)  DOImageView* tipView;
@end

@implementation HDAppointViewController

- (void)initUI
{
    [self.view addSubview:self.appointList.view];
    [self.view addSubview:self.tipView];
}


-(DOImageView*)tipView
{
    if (_tipView==nil)
    {
        float x=(APPFRAME.size.width-216)/2.0;
        float y=(SCREENFRAME.size.height-222)/2.0;
        CGRect rect=CGRectMake(x,y, 216, 118);
        _tipView=[[DOImageView alloc] initWithFrame:rect];
        _tipView.imgURLStr=@"bg0";
    }
    return _tipView;
}

- (void)setOrderType:(HDOrderType)orderType
{
    _orderType = orderType;
    _appointList.orderType = self.orderType;
}

- (void)pullTableView:(UITableView *)tableView isNextPage:(BOOL)isNextPage
{
    self.appointList.pageNum++;
    [self requestAppointlist:YES];
}

- (void)pullRefreshRequest
{
    self.appointList.pageNum = 1;
    [self requestAppointlist:YES];
}

-(void)requestAppointlist:(BOOL)isPull
{
    NSString *pageNo = [NSString stringWithFormat:@"%d",self.appointList.pageNum];
    UIView* hudView=isPull?nil:self.appointList.view;
    NSDictionary* params=@{
                            @"coach_id":netEngine.coachInfo.m_id,
                            @"order_type":[NSNumber numberWithInt:self.orderType],//:1：教练 2：陪练://需要根据是学车界面还是练车界面选择
                            @"state":[NSNumber numberWithInt:3],
                            @"order_category":[NSNumber numberWithInt:2],
                            @"page":pageNo,
                            @"row":@"20"
                           };
    [netEngine RequestOrderListWithTarget:self.VctrID hudView:hudView Data:params];
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==101&&buttonIndex==1)
    {
        NSNumber *stateNumber = [NSNumber numberWithInt:5];
        //对指定单操作:拒绝
        NSDictionary* params=@{
                               @"coach_id":netEngine.coachInfo.m_id,
                               @"id":self.orderId,//订单id
                               @"state":stateNumber,//:4：接单 5：拒绝://需要根据是用户操作选择
                               };
        [netEngine RequestTargetOrderOpWithTarget:self.VctrID hudView:self.contentView Data:params];
    }
}


- (void)handleAppointOrder:(UIButton *)sender orderId:(NSString *)orderId
{
    self.orderId=orderId;
    if ((sender.tag==2000))
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"拒绝该指定单" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag=101;
        [alert show];
    }
    else
    {
        NSNumber *stateNumber = [NSNumber numberWithInt:4];
        //对指定单操作:接单
        NSDictionary* params=@{
                               @"coach_id":netEngine.coachInfo.m_id,
                               @"id":self.orderId,//订单id
                               @"state":stateNumber,//:4：接单 5：拒绝://需要根据是用户操作选择
                               };
        [netEngine RequestTargetOrderOpWithTarget:self.VctrID hudView:self.contentView Data:params];
    }
}

#pragma mark - getters
- (HDAppointTableViewController *)appointList
{
    if (!_appointList) {
        _appointList = [[HDAppointTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
        _appointList.tableView.frame = CGRectMake(0, 0, APPFRAME.size.width, self.view.height);
        _appointList.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _appointList.tableView.backgroundColor = [UIColor clearColor];
        _appointList.orderType = self.orderType;
        _appointList.delegate = (id)self;
        _appointList.enableRefresh = YES;
        _appointList.enableRequest = YES;
    }
    return _appointList;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.appointList.pageNum = 1;
    [self requestAppointlist:NO];
}


-(void)updateViewController:(NSNotification *)note
{
    NSDictionary* dic=note.userInfo;
    if ([dic objectForKey:Receiver]==self.VctrID)
    {
        if (dic[@"OrderList"]!=nil)
        {
            self.appointList.totalPagesNum = [[dic objectForKey:@"totalPage"] intValue];
            if (self.appointList.pageNum==1)
            {
                self.appointList.dataArray = nil;
            }
            NSMutableArray *dataArray = [[NSMutableArray alloc] initWithArray:self.appointList.dataArray];
            [dataArray addObjectsFromArray:dic[@"dataArray"]];
            self.appointList.dataArray = dataArray;
            self.tipView.hidden=self.appointList.dataArray.count;
        }
        else if (dic[@"TargetOrderOp"]!=nil)
        {
            [self requestAppointlist:NO];
        }
    }
}

@end
